﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;

[assembly: TagPrefix("Azad.BCL.CustomGrid", "CustomGrid")]
namespace Azad.BCL.CustomGrid
{
    public class CustomBoundField : DataControlField
    {
        #region For Link Button
        [Description("Navigation URL")]
        public string CustomNavigationURL
        {
            get
            {
                object value = ViewState["CustomNavigationURL"];

                if (value != null)
                {
                    return value.ToString();
                }

                return string.Empty;
            }

            set
            {
                ViewState["CustomNavigationURL"] = value;

                OnFieldChanged();
            }
        }

        [Description("Text of Navigation")]
        public string CustomNavigationText
        {
            get
            {
                object value = ViewState["CustomNavigationText"];

                if (value != null)
                {
                    return value.ToString();
                }

                return string.Empty;
            }

            set
            {
                ViewState["CustomNavigationText"] = value;

                OnFieldChanged();
            }
        }

        [Description("Navigation URL")]
        public bool isNavigationBound
        {
            get
            {
                object value = ViewState["isNavigationBound"];

                if (value != null)
                {
                    return Convert.ToBoolean(value);
                }

                return false;
            }

            set
            {
                ViewState["isNavigationBound"] = value;

                OnFieldChanged();
            }
        }
        #endregion
        public bool IsFilterApply
        {
            get
            {
                object value = ViewState["isFilterApply"];

                if (value != null)
                {
                    return Convert.ToBoolean(value);
                }

                return false;
            }

            set
            {
                ViewState["isFilterApply"] = value;

            }
        }

        public string SufixWithDot
        {
            get
            {
                object value = ViewState["SufixWithDot"];

                if (value != null)
                {
                    return (value).ToString();
                }

                return "";
            }

            set
            {
                ViewState["SufixWithDot"] = value;

            }
        }

        public DataType FilterDataType
        {
            get
            {
                object value = ViewState["DataType"];

                if (value != null)
                {
                    return (DataType)value;
                }

                return DataType.String;
            }

            set
            {
                ViewState["DataType"] = value;

            }
        }

        public string DataField
        {
            get
            {
                object value = ViewState["DataField"];

                if (value != null)
                {
                    return value.ToString();
                }

                return string.Empty;
            }

            set
            {
                ViewState["DataField"] = value;

                OnFieldChanged();
            }
        }

        [Description("Replace False Value on given text")]
        public string FalseText
        {
            get
            {
                object value = ViewState["FalseText"];

                if (value != null)
                {
                    return value.ToString();
                }

                return string.Empty;
            }

            set
            {
                ViewState["FalseText"] = value;

                OnFieldChanged();
            }
        }
        [Description("Replace True Value on given text")]
        public string TrueText
        {
            get
            {
                object value = ViewState["TrueText"];

                if (value != null)
                {
                    return value.ToString();
                }

                return string.Empty;
            }

            set
            {
                ViewState["TrueText"] = value;

                OnFieldChanged();
            }
        }

        public bool isMandatory
        {
            get
            {
                object value = ViewState["isMandatory"];

                if (value != null)
                {
                    return Convert.ToBoolean(value);
                }

                return false;
            }

            set
            {
                ViewState["isMandatory"] = value;

            }
        }

        public ValidationType ValidationType
        {
            get
            {
                object value = ViewState["ValidationType"];

                if (value != null)
                {
                    return (ValidationType)value;
                }

                return ValidationType.String;
            }

            set
            {
                ViewState["ValidationType"] = value;

            }
        }

        public string CustomValidationExpressions
        {
            get
            {
                object value = ViewState["ValidationExpressions"];

                if (value != null)
                {
                    return value.ToString();
                }

                return string.Empty;
            }

            set
            {
                ViewState["ValidationExpressions"] = value;

                OnFieldChanged();
            }
        }

        #region " Methods "

        protected override DataControlField CreateField()
        {
            return new CustomBoundField();
        }

        public override void ExtractValuesFromCell(System.Collections.Specialized.IOrderedDictionary dictionary, DataControlFieldCell cell, DataControlRowState rowState, bool includeReadOnly)
        {
            base.ExtractValuesFromCell(dictionary, cell, rowState, includeReadOnly);
            string value = null;

            if (cell.Controls.Count > 0)
            {
                Control control = cell.Controls[0];
                //If the control is null, throw an error
                if (control == null)
                    throw new InvalidOperationException("The control cannot be extracted");
                value = ((TextBox)control).Text;
            }

            //If the key exists, update the value
            if (dictionary.Contains(this.DataField))
                dictionary[this.DataField] = value;
            //Add a new entry to the dictionary
            else
                dictionary.Add(this.DataField, value);
        }

        public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
        {
            base.InitializeCell(cell, cellType, rowState, rowIndex);
            Control control = null;

            if (cellType == DataControlCellType.DataCell)
            {
                if (this.IsReadMode(rowState))
                    control = cell;

            }

            //Capture the data binding event when data binding
            if (control != null && this.Visible)
                control.DataBinding += new EventHandler(control_DataBinding);
        }

        protected bool IsReadMode(DataControlRowState rowState)
        {
            return (rowState == DataControlRowState.Normal || rowState == DataControlRowState.Alternate || rowState == DataControlRowState.Selected);
        }

        #endregion

        #region " Event Handlers "

        void control_DataBinding(object sender, EventArgs e)
        {
            if (sender is TableCell)
            {

                if (this.isNavigationBound)
                {
                    TableCell cell = sender as TableCell;
                    object dataItem = DataBinder.GetDataItem(cell.NamingContainer);
                    string _text = DataBinder.GetPropertyValue(dataItem, this.DataField, null);
                    string _DisplayText = DataBinder.GetPropertyValue(dataItem, this.CustomNavigationText, null);
                    LinkButton lnk = new LinkButton();
                    lnk.Text = _DisplayText;//this.Text;

                    lnk.OnClientClick = "javascript:window.location ='" + Control.ResolveUrl(this.CustomNavigationURL) + _text + "'; return false;";
                    lnk.ID = "a" + cell.Text;
                    cell.Controls.Add(lnk);
                }
                else
                {

                    TableCell cell = sender as TableCell;
                    object dataItem = DataBinder.GetDataItem(cell.NamingContainer);
                    cell.Text = DataBinder.GetPropertyValue(dataItem, this.DataField, null);
                    if (this.FilterDataType == DataType.Boolean)
                    {
                        if (cell.Text == "True" || cell.Text == "1")
                            cell.Text = "1";
                        else
                            cell.Text = "0";
                        cell.Text = cell.Text != "0" ? Convert.ToBoolean(Convert.ToInt32(cell.Text)) == true ? String.IsNullOrEmpty(TrueText) ? cell.Text : TrueText : cell.Text : FalseText;
                        //cell.Text = String.IsNullOrEmpty(cell.Text) == false ? Convert.ToBoolean(int.Parse(cell.Text)) == false ? String.IsNullOrEmpty(FalseText) ? cell.Text : FalseText : cell.Text : FalseText;
                    }
                }
            }

        }

        #endregion
    }
}
